<?php
class Room extends CI_Model{
	
	function search_reserve($s, $bid){
		
			$get = $this->db->query("SELECT * FROM reservations r
		                       JOIN branches b ON r.branch_id = b.branch_id
							   JOIN customers c ON r.customer_id = c.customer_id
							   WHERE r.branch_id = $bid AND ( c.firstname LIKE '$s%' OR c.firstname LIKE '%$s' OR
							   c.firstname LIKE '%$s%' OR c.lastname LIKE '$s%' OR
							   c.lastname LIKE '%$s' OR c.lastname LIKE '%$s%' )
							   AND r.reserve_id NOT IN (
								SELECT reservation_id FROM check_in
							   ) AND r.reserve_id NOT IN (SELECT reservation_id FROM check_out)");
		
	
		if($get->num_rows() > 0){
			return $get->result();
		}
		else{
			return "NONE";
		}
	}
	
	function insert_checkin($data){
		$insert = $this->db->insert('check_in', $data);
	}
	
	function delete_checkin($data){
		$this->db->delete('check_in', $data);
	}
	
	function get_checkin_list(){
		$checkin_list = $this->db->get('check_in');
		if($checkin_list->num_rows() > 0){
			return $checkin_list->result();
		}
		else{
			return FALSE;
		}
	}
	
	function search_checkin($s, $bid){
		$get = $this->db->query("SELECT * FROM reservations r
				JOIN branches b ON r.branch_id = b.branch_id
				JOIN customers c ON r.customer_id = c.customer_id
				JOIN check_in i ON i.reservation_id = r.reserve_id
				WHERE r.branch_id = $bid AND ( c.firstname LIKE '$s%' OR c.firstname LIKE '%$s' OR
				c.firstname LIKE '%$s%' OR c.lastname LIKE '$s%' OR
				c.lastname LIKE '%$s' OR c.lastname LIKE '%$s%' )
				AND r.reserve_id NOT IN (
				SELECT reservation_id FROM check_out
				)");
		if($get->num_rows() > 0){
			return $get->result();
		}
		else{
			return FALSE;
		}
	}
	
	function insert_checkout($data){
		$insert = $this->db->insert('check_out', $data);
	}
	
	function delete_reservation($id){
		$this->db->delete('reservations', array('reserve_id'=>$id));
	}
}